package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.FlashEntry;
import com.pro.springbootshop.entityExtend.FlashEntryReturn;
import com.pro.springbootshop.mapper.FlashEntryMapper;
import com.pro.springbootshop.service.FlashEntryService;
import com.pro.springbootshop.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FlashEntryServiceImpl implements FlashEntryService {
    @Autowired
    TokenUtil tokenUtil;
    public FlashEntryMapper flashEntryMapper;
    @Autowired
    public void setUserMapper (FlashEntryMapper flashEntryMapper) {
        this.flashEntryMapper = flashEntryMapper;
    }
    @Override
    public String saveFlashEntry(FlashEntry flashEntry, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        System.out.println("-------------buyerId"+buyerId);
        flashEntry.setBuyerId(buyerId);
        System.out.println("-------------flashActivityId"+flashEntry.getFlashActivityId());
        int i=flashEntryMapper.insert(flashEntry);
        if(i>0){
            return "提交成功";
        }else {
            return "提交失败";
        }
    }

    @Override
    public FlashEntryReturn getAllFlashEntry(Integer page, Integer limit, Integer flashActivityId) {
        int pageNo=(page-1)*limit;
        int total=flashEntryMapper.selectCount(null);
        List<FlashEntry> flashEntryList=flashEntryMapper.getAllFlashEntry(pageNo,limit,flashActivityId);
        return new FlashEntryReturn(flashEntryList,total);
    }


    @Override
    public String buyergetFlashEntry(String token,Integer flashActivityId) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        List<FlashEntry> flashEntryList=flashEntryMapper.buyergetFlashEntry(buyerId,flashActivityId);
        if(!flashEntryList.isEmpty()){
            return "报名成功";
        }else {
            return "报名失败";
        }
    }
    @Override
    public FlashEntryReturn getAllFlashEntry_admin(Integer page, Integer limit) {
        int pageNo=(page-1)*limit;
        int total=flashEntryMapper.selectCount(null);
        List<FlashEntry> flashEntryList=flashEntryMapper.getAllFlashEntry_admin(pageNo,limit);
        return new FlashEntryReturn(flashEntryList,total);
    }
    @Override
    public FlashEntryReturn buyerIdGetFlashEntry(Integer page, Integer limit, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        int pageNo=(page-1)*limit;
        int total=flashEntryMapper.selectCount(null);
        List<FlashEntry> flashEntryList=flashEntryMapper.buyerIdGetFlashEntry(pageNo,limit,buyerId);
        return new FlashEntryReturn(flashEntryList,total);
    }
    @Override
    public String changeFlashEntry(Integer flashEntryId,Integer buyState) {
        FlashEntry flashEntry=flashEntryMapper.selectById(flashEntryId);
        flashEntry.setBuyState(buyState);
        int i=flashEntryMapper.updateById(flashEntry);
        if(i>0){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
}